package com.ciwong.epaper.modules.epaper.util;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.NewBook;
import com.ciwong.epaper.util.DisplayUtils;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * Created by Administrator on 2017/12/25.
 */

public class ItemBookOrService extends LinearLayout {


	private TextView tv_book_name;

	private TextView tv_book_grade;

	private SimpleDraweeView iv_book_icon;

	private TextView tv_buy_count;

	private NewBook mNewBook;


	public ItemBookOrService(Context context) {
		super(context);
		LayoutInflater.from(this.getContext()).inflate(R.layout.item_book_or_service, this);

		tv_book_name = (TextView) findViewById(R.id.tv_book_name);
		tv_book_grade = (TextView) findViewById(R.id.tv_book_grade);
		iv_book_icon = (SimpleDraweeView) findViewById(R.id.iv_book_icon);
		tv_buy_count = (TextView) findViewById(R.id.tv_buy_count);

		findViewById(R.id.item_book_or_service).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {


				if (null == mNewBook) {
					return;
				}
			}
		});

	}


	public void update(NewBook newBook) {
		mNewBook = newBook;

		LinearLayout.LayoutParams txNamePara;
		txNamePara = (LayoutParams) iv_book_icon.getLayoutParams();
		tv_book_name.setText(newBook.getServiceName());

		if (newBook.getSalesType() == 1) {
			//书籍
			tv_book_grade.setText("适用于：" + newBook.getGradeName());
			txNamePara.height = DisplayUtils.dip2px(getContext(), 86);
		} else {
			tv_book_grade.setText("适用于：" + newBook.getPeriod());
			txNamePara.height = DisplayUtils.dip2px(getContext(), 60);
		}
//		RoundingParams bitmapOnlyParams = RoundingParams.fromCornersRadius(10).setRoundingMethod(RoundingParams.RoundingMethod.BITMAP_ONLY);
//		iv_book_icon.getHierarchy().setRoundingParams(bitmapOnlyParams);
//        iv_book_icon.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
//		iv_book_icon.setImageURI(uri);
		if(!TextUtils.isEmpty(newBook.getLogo())){
			Uri uri = Uri.parse(newBook.getLogo());
			setNetImg(iv_book_icon,uri);
		}
		tv_buy_count.setText(newBook.getBuyNum() + "人已购买");
		iv_book_icon.setLayoutParams(txNamePara);

	}
	private void setNetImg(SimpleDraweeView simpleDraweeView, Uri uri) {
		try {
			ImageRequest request = null;
			if (uri != null) {
				request = ImageRequestBuilder.newBuilderWithSource(uri)
						.setResizeOptions(new ResizeOptions(DisplayUtils.dip2px(getContext(), 300), DisplayUtils.dip2px(getContext(), 420)))
						.build();
				RoundingParams bitmapOnlyParams = RoundingParams.fromCornersRadius(10)
						.setRoundingMethod(RoundingParams.RoundingMethod.OVERLAY_COLOR)
						.setOverlayColor(Color.WHITE);
				GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources());
				//			builder.setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
				builder.setRoundingParams(bitmapOnlyParams);
				GenericDraweeHierarchy hierarchy = builder.build();
				hierarchy.setPlaceholderImage(R.mipmap.item_load_icon);
				simpleDraweeView.setHierarchy(hierarchy);
				DraweeController drawweeController = Fresco.newDraweeControllerBuilder()
						.setUri(uri)
						.setImageRequest(request)
						//列表滑动会变流畅
						.setOldController(simpleDraweeView.getController())
						.build();
				simpleDraweeView.setController(drawweeController);
			}
		}catch (Exception e){

		}catch (OutOfMemoryError outOfMemoryError){

		}

	}

}
